package com.bwie.util;

public class PageUtil {

	//属性封装之提供条件
	private int currentPage;  //当前页
	private int totalCount;   //总条数
	private int pageSize;     //分页单位

	//属性封装之需要计算条件
	private int prevPage;     //上一页
	private int nextPage;     //下一页
	private int totalPage;    //尾页（总页数）
	private int startIndex;   //索引值ֵ （ (当前页-1)*分页单位 ）
	
	
	//构造方法，三个必须参数：当前页、总记录数、分页单位
	public PageUtil(String page, int totalCount, int pageSize){
		
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		initTotalPage();
		initCurrentPage(page);
		initPrevPage();
		initNextPage();
		initStartIndex();
	}
	

	/*
	 * 计算起始索引值
	 */
	private void initStartIndex() {
		startIndex = (currentPage - 1)*pageSize;
	}

	/*
	 * 计算下一页
	 */
	private void initNextPage() {
		nextPage = currentPage == totalPage ? totalPage : (currentPage + 1);
	}



	/*
	 * 计算尾页
	 */
	private void initTotalPage() {
		totalPage = (totalCount/pageSize)+(totalCount%pageSize == 0 ? 0 : 1);
		totalPage = totalPage == 0 ? 1 : totalPage;
	}

	/*
	 * 计算上一页
	 */
	private void initPrevPage() {
		prevPage = currentPage == 1 ? 1 : currentPage - 1;
	}


	/*
	 * 初始化当前页
	 */
	private void initCurrentPage(String page) {
		if(page == null||"".equals(page)){
			page = "1";
		}
		this.currentPage = Integer.parseInt(page);
	}
/*	private void initCurrentPage(String page) {
		if(page == null){
			page = "1";
		}
		this.currentPage = Integer.parseInt(page);
		
//		if(currentPage > totalPage){
//			this.currentPage = totalPage;
//		}
		
	}*/


	public int getCurrentPage() {
		return currentPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getStartIndex() {
		return startIndex;
	}
}
